<?php namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;

abstract class Controller extends BaseController {

	use DispatchesCommands, ValidatesRequests;

	/**
	 * 修改验证失败格式
	 */
	protected function buildFailedValidationResponse(Request $request, array $errors)
	{
		$error = head(head($errors));

		if ($request->ajax() || $request->wantsJson()) {
			return response()->json([
				'status' => 402,
				'message' => $error,
				'data' => (object) null
			]);
		}

		return redirect()->to($this->getRedirectUrl())
			->withInput($request->input())
			->withErrors($errors, $this->errorBag())
			->withMessageError($error);
	}

}
